#!/bin/sh

# 配置源
add_repo() {
  cat >>/etc/apk/repositories <<"EOF"
http://mirrors.aliyun.com/alpine/edge/community
http://mirrors.aliyun.com/alpine/edge/testing
EOF
}
conf_repo() {
  sed -i "s/^#http/http/" /etc/apk/repositories
  grep edge /etc/apk/repositories ||  add_repo
}
# 配置时间同步
conf_ntp() {
    apk add tzdata
  # 配置时区
  install -Dm 0644 /usr/share/zoneinfo/Asia/Shanghai /etc/zoneinfo/Asia/Shanghai
  export TZ='Asia/Shanghai'
  echo "export TZ='$TZ'" >>/etc/profile.d/timezone.sh
  sed -i "s|pool.ntp.org|ntp.aliyun.com|g" /etc/chrony/chrony.conf
  rc-service chronyd restart
}
# bash 环境
conf_bash() {
  apk add bash bash-completion
  # 配置 bash
  sed -i 's/ash/bash/g' /etc/passwd
  cat >/etc/profile.d/self.sh <<"eof"
alias update='apk update && apk upgrade'
export HISTTIMEFORMAT="%d/%m/%y %T "
export PS1='\u@\h:\W \\$ '
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
source /usr/share/bash-completion/bash_completion
eof
}
# 通用软件包安装
common_package() {
  packages="curl vim wget git"
  for package in ${packages}; do
    apk add ${package}
  done
}
# k8s软件包安装
k8s_package() {
  packages="cni-plugin-flannel cni-plugins flannel flannel-contrib-cni kubelet kubeadm kubectl containerd containerd-ctr uuidgen nfs-utils"
  for package in ${packages}; do
    apk add ${package}
  done
}
# k8s镜像拉取
k8s_pull() {
  grep aliyun /etc/containerd/config.toml >>/dev/null || curl -o /etc/containerd/config.toml https://gitee.com/chongxs/opscript/raw/master/config/containerd.toml
  rc-service containerd restart
  kubeadm config images pull --image-repository registry.aliyuncs.com/google_containers
}
# k8s初始化
k8s_init() {
  if [ $(hostname) == "localhost" ]; then
    echo "Please modify the hostname"
    exit 1
  else
    mem=$(free | awk 'NR==2{print $2}')
    cores=$(grep "cpu cores" /proc/cpuinfo | uniq | cut -d: -f2)
    if [[ $mem -gt 204800 ]] && [[ $cores -gt 1 ]]; then
      echo "Start initialization"
      kubeadm init --node-name=$(hostname) --image-repository registry.aliyuncs.com/google_containers
    else
      echo "If the configuration is low, upgrade the configuration"
    fi
  fi
}
# k8s打印加入信息
k8s_print() {
  pass
}
# k8s网络组件安装
k8s_cni() {
  curl -O https://mirror.ghproxy.com/https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
  kubectl apply -f kube-flannel.yml
}

k8s_env() {
        # 关闭swap
    swapoff -a
    sed -i "s|.*swap.*|#&|" /etc/fstab

    # 加载模块
    echo "br_netfilter" >/etc/modules-load.d/k8s.conf
    modprobe br_netfilter
  # 优化内核
    cat >>/etc/sysctl.conf <<"EOF"
net.bridge.bridge-nf-call-iptables=1
net.ipv4.ip_forward= 1
EOF
    sysctl -p

    # 处理监控
    mount --make-rshared /
    echo "#!/bin/sh" >/etc/local.d/sharemetrics.start
    echo "mount --make-rshared /" >>/etc/local.d/sharemetrics.start
    chmod +x /etc/local.d/sharemetrics.start
    rc-update add local
    #Fix id error messages
    uuidgen >/etc/machine-id

    #Add services
    #rc-update add docker
    rc-update add containerd
    rc-update add kubelet
    rc-service containerd start
}
k8s_config() {
   mkdir -p $HOME/.kube
   cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
   chown $(id -u):$(id -g) $HOME/.kube/config
   export KUBECONFIG=/etc/kubernetes/admin.conf
}

choices=("配置源" "更新" "ntp同步" "配置bash" "基础软件" "k8s组件" "k8s镜像" "k8s初始化" "k8s网络组件" "k8s工作节点" "k8s打印加入" "退出")

select opt in "${choices[@]}";do
  case $opt in
  配置源)
    conf_repo
    ;;
  更新)
    apk update
    apk upgrade
    ;;
  ntp同步)
    grep aliyun /etc/chrony/chrony.conf >/dev/null || conf_ntp
    ;;
  配置bash)
    if [ $SHELL != "/bin/bash" ]; then
      conf_bash
      echo "Switch the shell and take effect after logging in again."
      exit 0
    fi
    ;;
  基础软件)
    common_package
    ;;
  k8s组件)
    k8s_package
    k8s_env
    ;;
  k8s镜像)
    k8s_pull
    ;;
  k8s初始化)
    k8s_init
    ;;
  k8s网络组件)
    k8s_config
    k8s_cni
    ;;
  k8s工作节点)
    conf_repo
    conf_ntp
    if [ $SHELL != "/bin/bash" ]; then
      conf_bash
      echo "Switch the shell and take effect after logging in again."
      exit 0
    fi
    common_package
    k8s_package
    k8s_env
    k8s_pull
    ;;
  k8s打印加入)
    kubeadm token create --print-join-command
    ;;
  退出)
    echo "退出"
    break
    ;;
  *)
    echo "请键入正确的数字选项:"
    cat <<"EOF"
1) 配置源         4) 配置bash      7) k8s镜像      10) k8s工作节点
2) 更新           5) 基础软件      8) k8s初始化    11) k8s打印加入
3) ntp同步        6) k8s组件       9) k8s网络组件  12) 退出
EOF
    ;;
  esac
done